/**
* This file is part of OntoApplications (project linked with OntoCompoFramework)
*
* Copyright (C) 2011 -  Christian Brel
*
* @author   Main    Christian Brel
**/
package applications.insurancecard.ui.lib;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import ontocompo.lib.PartialBorder;
import ontocompo.lib.PartialBorder.Side;

import org.objectweb.fractal.fraclet.annotations.Component;
import org.objectweb.fractal.fraclet.annotations.Interface;
import org.objectweb.fractal.fraclet.annotations.Requires;

import applications.insurancecard.fc.api.InsuranceInformation;

@Component(provides = {@Interface(name = "jPanelItf", signature = org.objectweb.fractal.swing.JPanelItf.class), @Interface(name = "jPanelAttributes", signature = org.objectweb.fractal.swing.JPanelAttributes.class), @Interface(name = "uicontainerservice", signature = ontocompo.api.UIContainerService.class), @Interface(name = "observer", signature = java.util.Observer.class), @Interface(name = "ontocompointrospection", signature = ontocompo.api.OntoCompoIntrospection.class)})
public class InsuranceCardSocialInformationPanel extends JPanel implements /*ContainerService,*/ org.objectweb.fractal.swing.JPanelItf, org.objectweb.fractal.swing.JPanelAttributes, ontocompo.api.UIContainerService, java.util.Observer, ontocompo.api.OntoCompoIntrospection {

	private String uid;
	private org.objectweb.fractal.api.Component c;
	
	//TODO
	@Requires(name = "socialinfolastnamelabel")
	private ontocompo.api.UIContainerService socialinfolastnamelabel;
	
	@Requires(name = "socialinfolastnamevalue")
	private ontocompo.api.UIContainerService socialinfolastnamevalue;
	
	@Requires(name = "socialinfofirstnamelabel")
	private ontocompo.api.UIContainerService socialinfofirstnamelabel;
	
	@Requires(name = "socialinfofirstnamevalue")
	private ontocompo.api.UIContainerService socialinfofirstnamevalue;
	
	@Requires(name = "socialinfobirthdaylabel")
	private ontocompo.api.UIContainerService socialinfobirthdaylabel;
	
	@Requires(name = "socialinfobirthdayvalue")
	private ontocompo.api.UIContainerService socialinfobirthdayvalue;
	
	@Requires(name = "socialinfobirthplacelabel")
	private ontocompo.api.UIContainerService socialinfobirthplacelabel;
	
	@Requires(name = "socialinfobirthplacevalue")
	private ontocompo.api.UIContainerService socialinfobirthplacevalue;
	
	@Requires(name = "socialinfobirthcountrylabel")
	private ontocompo.api.UIContainerService socialinfobirthcountrylabel;
	
	@Requires(name = "socialinfobirthcountryvalue")
	private ontocompo.api.UIContainerService socialinfobirthcountryvalue;
	
	@Requires(name = "socialinfoinsurancenumberlabel")
	private ontocompo.api.UIContainerService socialinfoinsurancenumberlabel;
	
	@Requires(name = "socialinfoinsurancenumbervalue")
	private ontocompo.api.UIContainerService socialinfoinsurancenumbervalue;
	
	@Requires(name = "socialinfoinsurancekeylabel")
	private ontocompo.api.UIContainerService socialinfoinsurancekeylabel;
	
	@Requires(name = "socialinfoinsurancekeyvalue")
	private ontocompo.api.UIContainerService socialinfoinsurancekeyvalue;
	
	@Requires(name = "socialinfomedicalreferentlabel")
	private ontocompo.api.UIContainerService socialinfomedicalreferentlabel;
	
	@Requires(name = "socialinfomedicalreferentvalue")
	private ontocompo.api.UIContainerService socialinfomedicalreferentvalue;
	//TODO
	
	
	@Requires(name = "insurancecardservice")
	private applications.insurancecard.fc.api.InsuranceCardService insurancecardservice;
	
	@Requires(name = "insurancecardobservable")
	private ontocompo.api.Observable insurancecardobservable;
	
	public InsuranceCardSocialInformationPanel() {
		super();
		this.uid = "applications.insurancecard.ui.lib.InsuranceCardSocialInformationPanel";
		System.out.println(this.toString() + " created");
		//this.init();
	}
	
	public void init() {
		this.insurancecardobservable.addObserver(this);
		
		this.socialinfolastnamelabel.init();
		this.socialinfolastnamevalue.init();
		this.socialinfofirstnamelabel.init();
		this.socialinfofirstnamevalue.init();
		this.socialinfobirthdaylabel.init();
		this.socialinfobirthdayvalue.init();
		this.socialinfobirthplacelabel.init();
		this.socialinfobirthplacevalue.init();
		this.socialinfobirthcountrylabel.init();
		this.socialinfobirthcountryvalue.init();
		this.socialinfoinsurancenumberlabel.init();
		this.socialinfoinsurancenumbervalue.init();
		this.socialinfoinsurancekeylabel.init();
		this.socialinfoinsurancekeyvalue.init();
		this.socialinfomedicalreferentlabel.init();
		this.socialinfomedicalreferentvalue.init();
		
		this.setBackground(Color.white);
		
		PartialBorder pb = new PartialBorder();
		pb.setPadding(10);
		pb.setSide(Side.BOTTOM, true, 1);
		this.setBorder(pb);
		
		this.setLayout(new BorderLayout());
		this.add("North", createTitle("Social Insurance Account"));
		
		JPanel grid = createBox(1);
		grid.setLayout(new GridLayout(1, 4));
		
		/*grid.add(this.socialinfolastnamelabel.getComponentItSelf());
		grid.add(this.socialinfolastnamevalue.getComponentItSelf());
		grid.add(this.socialinfofirstnamelabel.getComponentItSelf());
		grid.add(this.socialinfofirstnamevalue.getComponentItSelf());
		grid.add(this.socialinfobirthdaylabel.getComponentItSelf());
		grid.add(this.socialinfobirthdayvalue.getComponentItSelf());
		grid.add(this.socialinfobirthplacelabel.getComponentItSelf());
		grid.add(this.socialinfobirthplacevalue.getComponentItSelf());
		grid.add(this.socialinfobirthcountrylabel.getComponentItSelf());
		grid.add(this.socialinfobirthcountryvalue.getComponentItSelf());
		grid.add(this.socialinfoinsurancenumberlabel.getComponentItSelf());
		grid.add(this.socialinfoinsurancenumbervalue.getComponentItSelf());
		grid.add(this.socialinfoinsurancekeylabel.getComponentItSelf());
		grid.add(this.socialinfoinsurancekeyvalue.getComponentItSelf());
		grid.add(this.socialinfomedicalreferentlabel.getComponentItSelf());
		grid.add(this.socialinfomedicalreferentvalue.getComponentItSelf());*/
		
		grid.add(createLine(BoxLayout.Y_AXIS, this.socialinfolastnamelabel.getComponentItSelf(), this.socialinfobirthdaylabel.getComponentItSelf(), new JLabel(" "), this.socialinfoinsurancenumberlabel.getComponentItSelf(), this.socialinfomedicalreferentlabel.getComponentItSelf()));
		grid.add(createLine(BoxLayout.Y_AXIS, this.socialinfolastnamevalue.getComponentItSelf(), this.socialinfobirthdayvalue.getComponentItSelf(), new JLabel(" "), this.socialinfoinsurancenumbervalue.getComponentItSelf(), this.socialinfomedicalreferentvalue.getComponentItSelf()));
		grid.add(createLine(BoxLayout.Y_AXIS, this.socialinfofirstnamelabel.getComponentItSelf(), this.socialinfobirthplacelabel.getComponentItSelf(), this.socialinfobirthcountrylabel.getComponentItSelf(), this.socialinfoinsurancekeylabel.getComponentItSelf(), new JLabel(" ")));
		grid.add(createLine(BoxLayout.Y_AXIS, this.socialinfofirstnamevalue.getComponentItSelf(), this.socialinfobirthplacevalue.getComponentItSelf(), this.socialinfobirthcountryvalue.getComponentItSelf(), this.socialinfoinsurancekeyvalue.getComponentItSelf(), new JLabel(" ")));
		
		this.add(grid);	
	}
	
	protected JPanel createBox(int thickness)
	{
		JPanel result = new JPanel();
		if (thickness > 0) result.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
		result.setBackground(Color.white);
		return result;
	}
//	
//	protected JLabel createTd(String txt)
//	{
//		JLabel result = new JLabel(txt);
//		result.setFont(result.getFont().deriveFont(Font.PLAIN));
//		result.setHorizontalAlignment(SwingConstants.LEFT);
//
//		return result;
//	}
//	
//	protected JLabel createTd(String txt, int thickness)
//	{
//		JLabel result = createTd(txt);
//		result.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
//		return result;
//	}
//	
//	protected JLabel createTh(String txt)
//	{
//		JLabel result = new JLabel(txt);
//		// result.setBorder(BorderFactory.createLineBorder(Color.blue, 1));
//
//		result.setHorizontalAlignment(SwingConstants.CENTER);
//		return result;
//	}
//	
//	protected JLabel createTh(String txt, int thickness)
//	{
//		JLabel result = createTh(txt);
//		result.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
//		return result;
//	}
//	
	protected JLabel createTitle(String txt)
	{
		JLabel result = new JLabel(txt);
		result.setHorizontalAlignment(SwingConstants.LEFT);
		result.setBorder(BorderFactory.createLineBorder(Color.black, 1));
		return result;
	}
	
	protected JPanel createLine(int axis, java.awt.Component... msgs)
	{
		JPanel result = createBox(0);
		result.setLayout(new BoxLayout(result, axis));
		for(java.awt.Component s : msgs)
		{			
			result.add(s);
		}
		
		return result;
	}
	
//	
//	protected JPanel createLine(int axis, int thickness, boolean title, String... msgs)
//	{
//		JPanel result = createBox(thickness);
//		result.setLayout(new BoxLayout(result, axis));
//		for(String s : msgs)
//		{
//			String msg = s;
//			if (s == "") msg = " ";
//			
//			JLabel lbl ;
//			if (title) lbl = createTh(msg);
//			else lbl = createTd(msg);
//			
//			lbl.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);
//			lbl.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
//			
//			result.add(lbl);
//		}
//		
//		return result;
//	}
//	
//	protected JPanel createLineLeftAlign(int axis, int thickness, boolean title, String... msgs)
//	{
//		JPanel result = createBox(thickness);
//		result.setLayout(new BoxLayout(result, axis));
//		for(String s : msgs)
//		{
//			String msg = s;
//			if (s == "") msg = " ";
//			
//			JLabel lbl ;
//			if (title) lbl = createTh(msg);
//			else lbl = createTd(msg);
//			
//			lbl.setAlignmentX(java.awt.Component.LEFT_ALIGNMENT);
//			lbl.setBorder(BorderFactory.createLineBorder(Color.black, thickness));
//			
//			result.add(lbl);
//		}
//		
//		return result;
//	}
	
//	@Override
	public JComponent getComponentItSelf() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public String getUid() {
		// TODO Auto-generated method stub
		return this.uid;
	}

	@Override
	public void setUid(String uid) {
		// TODO Auto-generated method stub
		this.uid = uid;
	}
	
	public String toString() {
		return "InsuranceCardSocialInformationPanel";
	}
/*
	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		InsuranceInformation ii = this.insurancecardservice.getInsuranceInfos();
		if(ii == null) {
			return ;
		} else {
			this.removeAll();
			
			this.setBackground(Color.white);
			
			PartialBorder pb = new PartialBorder();
			pb.setPadding(10);
			pb.setSide(Side.BOTTOM, true, 1);
			this.setBorder(pb);

			this.setLayout(new BorderLayout());
			this.add("North", createTitle("Social Insurance Account"));
			
			JPanel grid = createBox(1);
			grid.setLayout(new GridLayout(1, 4));
			
			grid.add(createLine(BoxLayout.Y_AXIS, 0, true, "Family Status"));
			grid.add(createLine(BoxLayout.Y_AXIS, 0, false, ii.getFamilystatus()));
			grid.add(createLine(BoxLayout.Y_AXIS, 0, true, "on the"));
			grid.add(createLine(BoxLayout.Y_AXIS, 0, false, ii.getFamilystatuschangedate()));
			
			this.add("Center", grid);
			
			
			grid = createBox(1);
			grid.setLayout(new GridLayout(1, 4));
			
			grid.add(createLine(BoxLayout.Y_AXIS, 0, true, "Adress", "", ""));
			
			JPanel detail = createBox(0);
			GridBagLayout dbl = new GridBagLayout();
			GridBagConstraints c = new GridBagConstraints();
			detail.setLayout(dbl);
			c.gridx = 0;
			c.gridy = 0;
			c.anchor = GridBagConstraints.CENTER;
			detail.add(createTh("Street"), c);
			c.gridx = 1;
			c.anchor = GridBagConstraints.WEST;
			detail.add(createTd(ii.getAdress().getStreet()), c);
			
			c.gridx = 0;
			c.gridy = 1;
			c.anchor = GridBagConstraints.CENTER;

			detail.add(createTh("Zip Code"), c);
			
			c.gridx = 1;
			c.anchor = GridBagConstraints.WEST;

			detail.add(createTd(ii.getAdress().getZipcode()), c);
			
			c.gridx = 0;
			c.gridy = 2;
			c.anchor = GridBagConstraints.CENTER;

			detail.add(createTh("Country"), c);
			c.gridx = 1;	
			c.weightx = 1;
			c.anchor = GridBagConstraints.WEST;
			detail.add(createTd(ii.getAdress().getZipcode()), c);
			
			grid.add( detail);
			
			grid.add(createLine(BoxLayout.Y_AXIS, 0, true, " ", " ", " "));
			grid.add(createLine(BoxLayout.Y_AXIS, 0, true, " ", " ", " "));

			this.add("South", grid);
			
			this.getParent().validate();
		}
	}
*/	
	@Override
	public void update(Observable arg0, Object arg1) {
		// TODO Auto-generated method stub
		InsuranceInformation ii = this.insurancecardservice.getInsuranceInfos();
		if(ii == null) {
			return ;
		} else {
			this.update(ii);
		}
	}
	
	@Override
	public void update(Object o) {
		InsuranceInformation ii = (InsuranceInformation) o;
		
		this.socialinfolastnamevalue.update(ii.getLastname());
		this.socialinfofirstnamevalue.update(ii.getFirstname());
		this.socialinfobirthdayvalue.update(ii.getBirthday());
		this.socialinfobirthplacevalue.update(ii.getBirthplace());
		this.socialinfobirthcountryvalue.update(ii.getCountry());
		this.socialinfoinsurancenumbervalue.update(ii.getNumber());
		this.socialinfoinsurancekeyvalue.update(ii.getKey());
		this.socialinfomedicalreferentvalue.update(ii.getMedicalreferent());
		
		this.removeAll();

		this.setBackground(Color.white);
		
		PartialBorder pb = new PartialBorder();
		pb.setPadding(10);
		pb.setSide(Side.BOTTOM, true, 1);
		this.setBorder(pb);
		
		this.setLayout(new BorderLayout());
		this.add("North", createTitle("Social Insurance Account"));
		
		JPanel grid = createBox(1);
		grid.setLayout(new GridLayout(1, 4));
		
		/*grid.add(this.socialinfolastnamelabel.getComponentItSelf());
		grid.add(this.socialinfolastnamevalue.getComponentItSelf());
		grid.add(this.socialinfofirstnamelabel.getComponentItSelf());
		grid.add(this.socialinfofirstnamevalue.getComponentItSelf());
		grid.add(this.socialinfobirthdaylabel.getComponentItSelf());
		grid.add(this.socialinfobirthdayvalue.getComponentItSelf());
		grid.add(this.socialinfobirthplacelabel.getComponentItSelf());
		grid.add(this.socialinfobirthplacevalue.getComponentItSelf());
		grid.add(this.socialinfobirthcountrylabel.getComponentItSelf());
		grid.add(this.socialinfobirthcountryvalue.getComponentItSelf());
		grid.add(this.socialinfoinsurancenumberlabel.getComponentItSelf());
		grid.add(this.socialinfoinsurancenumbervalue.getComponentItSelf());
		grid.add(this.socialinfoinsurancekeylabel.getComponentItSelf());
		grid.add(this.socialinfoinsurancekeyvalue.getComponentItSelf());
		grid.add(this.socialinfomedicalreferentlabel.getComponentItSelf());
		grid.add(this.socialinfomedicalreferentvalue.getComponentItSelf());*/
		
		grid.add(createLine(BoxLayout.Y_AXIS, this.socialinfolastnamelabel.getComponentItSelf(), this.socialinfobirthdaylabel.getComponentItSelf(), new JLabel(""), this.socialinfoinsurancenumberlabel.getComponentItSelf(), this.socialinfomedicalreferentlabel.getComponentItSelf()));
		grid.add(createLine(BoxLayout.Y_AXIS, this.socialinfolastnamevalue.getComponentItSelf(), this.socialinfobirthdayvalue.getComponentItSelf(), new JLabel(""), this.socialinfoinsurancenumbervalue.getComponentItSelf(), this.socialinfomedicalreferentvalue.getComponentItSelf()));
		grid.add(createLine(BoxLayout.Y_AXIS, this.socialinfofirstnamelabel.getComponentItSelf(), this.socialinfobirthplacelabel.getComponentItSelf(), this.socialinfobirthcountrylabel.getComponentItSelf(), this.socialinfoinsurancekeylabel.getComponentItSelf(), new JLabel("")));
		grid.add(createLine(BoxLayout.Y_AXIS, this.socialinfofirstnamevalue.getComponentItSelf(), this.socialinfobirthplacevalue.getComponentItSelf(), this.socialinfobirthcountryvalue.getComponentItSelf(), this.socialinfoinsurancekeyvalue.getComponentItSelf(), new JLabel("")));
		
		this.add(grid);	
		
		this.getParent().validate();
	}

	@Override
	public org.objectweb.fractal.api.Component getFractalComponent() {
		// TODO Auto-generated method stub
		return this.c;
	}

	@Override
	public void setFractalComponent(org.objectweb.fractal.api.Component c) {
		// TODO Auto-generated method stub
		this.c = c;
	}

}
